package com.opi.thread;

import org.apache.log4j.Logger;

import com.opi.bean.CpuBean;
import com.opi.dao.CpuDao;
import com.opi.util.CommUtil;
import com.opi.util.DateUtil;
import com.opi.util.ReadScript;

public class CpuCollectionThread extends Thread {
	
	private Logger logger = Logger.getLogger(CpuCollectionThread.class);
	//采样间隔
	private static long sleepTime = 15000; 
	
	public void run(){  
		CpuDao cDao = new CpuDao();
		CpuBean bean = null;
		String command = "";
		//采集cpu温度，cpu使用率
		String resp = "";
			while(true){
				try {
					Thread.sleep(sleepTime);
					//采样cpu温度
					command = "cat /sys/devices/virtual/thermal/thermal_zone0/temp";
					resp = CommUtil.getCpuTempOrLoadInfo(command);
					if(resp!=null){
						bean = new CpuBean(resp , DateUtil.getNowTime(1) , "t_cputemp");
						cDao.addCpuInfo(bean);
					}
				} catch (InterruptedException e) {
					logger.error("CpuCollectionThread-t_cputemp:"+e.getMessage());
				}
				try {
					Thread.sleep(sleepTime);
					//采样cpu使用率
					command = ReadScript.getCpuMonitor();
					resp = CommUtil.getCpuTempOrLoadInfo(command);
					if(resp!=null){
						bean = new CpuBean(resp , DateUtil.getNowTime(1) , "t_cpuload");
						cDao.addCpuInfo(bean);
					}
				} catch (InterruptedException e) {
					logger.error("CpuCollectionThread-t_cpuload:"+e.getMessage());
				}
			}
		
	}  
}
